<template>
  <view class="init-msg">{{ errorMsg }}</view>
</template>
  
  <script lang="ts" setup>
import { ref, getCurrentInstance, onMounted } from "vue";
import { Result, ResultError } from "@/types";
import { onLoad } from "@dcloudio/uni-app";
import { initDiyData } from "@/api/common";
import { useConfig } from "@/store";

const {
  proxy,
  proxy:{
    $utils
  }
} = getCurrentInstance() as any;

const initLoading = ref<boolean>(true);

const errorMsg = ref<string>("正在初始化...");

const toInit = () => {
  initDiyData()
    .then((res: Result) => {
      useConfig().setDiy(res.data);
      var pages = getCurrentPages(); // 获取页面栈
      var prevPage = pages[pages.length - 2]; // 上一个页面
      if (prevPage && prevPage.route !== "pages/index/init") {
        prevPage.onLoad && prevPage.onLoad()
        prevPage.onShow && prevPage.onShow()
        uni.navigateBack();
      } else {
        $utils.routerRedirectTo("/pages/index/index");
      }
    })
    .catch((err: ResultError) => {
      initLoading.value = false;
      errorMsg.value = err?.data?.message || "";
    });
};

onLoad(() => {
  toInit();
});
</script>
   
  <style scoped>
.init-msg {
  position: fixed;
  background: #fff;
  height: 100%;
  width: 100%;
  line-height: 500rpx;
  text-align: center;
}
</style>
  